﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameModels
{
    public class BillBoard : ModelBase
    {
        public BillBoard(Game game)
            : base(game)
        {
        }
        private Texture2D _Texture;
        private VertexPositionNormalTexture[] vertices;
        private short[] indies;
        private Vector3[] _Positions;
        private Effect _Effect;
        private string _View;
        private string _Projection, _TextureName, _World;
        public void CreateBillBoard(Texture2D texture, string textureParam, Vector3[] Positions, float[] heights, Effect effect, string world, string view, string projection)
        {
            _View = view;
            _Projection = projection;
            _World = world;
            _Positions = Positions;
            _Texture = texture;
            _TextureName = textureParam;
            _Effect = effect;
            vertices = new VertexPositionNormalTexture[Positions.Length * 6];
            int indexPerCount = 12;
            indies = new short[Positions.Length * indexPerCount];
            int count = 0;
            int index = 0;
            foreach (Vector3 position in Positions)
            {
                indies[index * indexPerCount + 0] = (short)(count);
                indies[index * indexPerCount + 1] = (short)(count + 1);
                indies[index * indexPerCount + 2] = (short)(count + 2);
                indies[index * indexPerCount + 3] = (short)(count + 2);
                indies[index * indexPerCount + 4] = (short)(count + 3);
                indies[index * indexPerCount + 5] = (short)(count);

                indies[index * indexPerCount + 6] = (short)(count);
                indies[index * indexPerCount + 7] = (short)(count + 4);
                indies[index * indexPerCount + 8] = (short)(count + 2);
                indies[index * indexPerCount + 9] = (short)(count + 2);
                indies[index * indexPerCount + 10] = (short)(count + 5);
                indies[index * indexPerCount + 11] = (short)(count);

                float w = _Texture.Width * heights[index] / texture.Height;
                VertexPositionNormalTexture v = new VertexPositionNormalTexture();
                v.Position = position;
                v.TextureCoordinate = new Vector2(0.5f, 1);
                vertices[count++] = v;
                v = new VertexPositionNormalTexture();
                v.Position = position + new Vector3(-w / 2, heights[index] / 2, 0);
                v.TextureCoordinate = new Vector2(0, 0.5f);
                vertices[count++] = v;
                v = new VertexPositionNormalTexture();
                v.Position = position + new Vector3(0, heights[index], 0);
                v.TextureCoordinate = new Vector2(0.5f, 0);
                vertices[count++] = v;
                v = new VertexPositionNormalTexture();
                v.Position = position + new Vector3(w / 2, heights[index] / 2, 0);
                v.TextureCoordinate = new Vector2(1, 0.5f);
                vertices[count++] = v;


                v = new VertexPositionNormalTexture();
                v.Position = position + new Vector3(0, heights[index] / 2, -w / 2);
                v.TextureCoordinate = new Vector2(0, 0.5f);
                vertices[count++] = v;
                v = new VertexPositionNormalTexture();
                v.Position = position + new Vector3(0, heights[index] / 2, w / 2);
                v.TextureCoordinate = new Vector2(1, 0.5f);
                vertices[count++] = v;


                index++;
            }
            Game.Components.Add(this);
        }
        public override void Draw(GameTime gameTime)
        {
            _Effect.Parameters[_TextureName].SetValue(_Texture);
            _Effect.Parameters[_View].SetValue(Matrix.CreateRotationY(MathHelper.PiOver2) * View);
            _Effect.Parameters[_Projection].SetValue(Projection);
            _Effect.Parameters[_World].SetValue(World);
            foreach (EffectPass et in _Effect.CurrentTechnique.Passes)
            {
                et.Apply();
                Game.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indies, 0, _Positions.Length);
            }
        }
    }
}
